<?php
namespace app\home\model;
use think\Model;
use think\Db;
class Api extends Model
{
    private $apiConfig = array(
        'account_id'    => 'test01',
        'account_key'   => 'fszyypatientlocate',
        'version'       => '1.0',
        'request_url'   => 'http://10.46.12.150:8820',
        );
    private $timestamp = '';
    private $timestamp2 = '';

    function __construct(){
        parent::__construct();
        $this->timestamp = time();
        $this->timestamp2 = date("YmdHis", $this->timestamp);
    }

    // 处理病人一段时间内的报警信息
    public function warning_handle($data){
        $method = 'warning_handle';
        $methodUrl = '/api/values/warning_handle';
        $requestBody = array();
        $requestBody['patient_id'] = $data['patient_id'];
        $requestBody['handle_time'] = date("Y-m-d H:i:s");
        $requestBody['handle_no'] = session('user_name');
        $request = array(
            'requestHead'   => $this->requestHead($method),
            'requestBody'   => $requestBody
            );
        addlog($request, 'warning_handle');
        $url = $this->apiConfig['request_url'].$methodUrl;
        $headers = array(
            // "Host: 10.46.12.150:8820",
            "Connection: keep-alive",
            "Pragma: no-cache",
            "Cache-Control: no-cache",
            "DNT: 1",
            "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36",
            "Content-type: application/json",
            "Accept: */*",
            "Origin: chrome-extension://aejoelaoggembcahagimdiliamlcdmfm",
            "Accept-Encoding: gzip, deflate",            
            "Accept-Language: zh-CN,zh;q=0.9,zh-TW;q=0.8",
        );
        $request = json_encode($request);
        $response = post_url($url, $request, $headers);
        addlog($response, 'warning_handle2');
        dump($response);
        $response = json_decode($response, true);
        $return = array();
        dump($response);
        if(is_array($response)){
            if( strlen($response['resultCode'])==1 && $response['resultCode']==0){
                $return['status'] = 1;
                $return['msg']  = '操作成功';
                // $return['msg']  = $response['message'];
            }else{
                $return['status'] = 0;
                $return['msg'] = $response['message'];
            }
        }else{
            $return['status'] = 0;
            $return['msg'] = '操作失败';
        }
        dump($return);
        return $return;
    }

    // 处理病人一段时间内的报警信息
    public function warning_handle2($data){
        $method = 'warning_handle';
        $methodUrl = '/api/values/warning_writeback';
        $requestBody = array();
        $requestBody['patient_id'] = '7810333';
        $requestBody['handle_time'] = '2021-01-19 10:11:12';
        $requestBody['handle_no'] = '4142';
        $request = array(
            'requestHead'   => array(
                'account_id'    => 'test01',
                'method'        =>  'warning_handle',
                'timestamp'     => '20201222101000',
                'version'       => '1.0',
                'sign'          => $this->makeSign($method)
                ),
            'requestBody'   => $requestBody
            );
        addlog($request, 'warning_handle');
        $url = $this->apiConfig['request_url'].$methodUrl;
        $headers = array(
            "Host: 10.46.12.150:8820",
            "Connection: keep-alive",
            "Pragma: no-cache",
            "Cache-Control: no-cache",
            "DNT: 1",
            "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36",
            "Content-type: application/json",
            "Accept: */*",
            "Origin: chrome-extension://aejoelaoggembcahagimdiliamlcdmfm",
            "Accept-Encoding: gzip, deflate",            
            "Accept-Language: zh-CN,zh;q=0.9,zh-TW;q=0.8",
        );
        $request = json_encode($request);
        $response = post_url($url, $request, $headers);
        addlog($response, 'warning_handle2');
        // $response = json_decode($response, true);
        // if(is_array($response) && $response['resultCode'] == 0){

        // }
    }

    public function syncWarning($data){
        $method = 'warning_writeback';
        $methodUrl = '/api/values/warning_writeback';
        $requestBody = array();
        $requestBody['patient_id'] = $data['patient_id'];
        $requestBody['department'] = $data['department'];
        $requestBody['rfid'] = $data['rfid'];
        $requestBody['createtime'] = date("Y-m-d H:i:s", $this->timestamp);
        $requestBody['location'] = $data['location'];
        $requestBody['equipment'] = $data['equipment'];
        $request = array(
            'requestHead'   => $this->requestHead($method),
            'requestBody'   => $requestBody
            );
        addlog($request, 'syncWarning');
        $url = $this->apiConfig['request_url'].$methodUrl;
        $headers = array(
            "Host: 10.46.12.150:8820",
            "Connection: keep-alive",
            "Pragma: no-cache",
            "Cache-Control: no-cache",
            "DNT: 1",
            "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36",
            "Content-type: application/json",
            "Accept: */*",
            "Origin: chrome-extension://aejoelaoggembcahagimdiliamlcdmfm",
            "Accept-Encoding: gzip, deflate",            
            "Accept-Language: zh-CN,zh;q=0.9,zh-TW;q=0.8",
        );
        $request = json_encode($request);
        $response = post_url($url, $request, $headers);
        addlog($response, 'syncWarning2', date("Ymd"));
        $response = json_decode($response, true);
        if(is_array($response) && $response['resultCode'] == 0){

        }
    }

    public function syncPatientList($page="1", $page_size="20", $department_code='', $starttime='', $endtime=''){
        $method = 'patient_list';
        $methodUrl = '/api/values/patient_list';
        $requestBody = array();
        $requestBody['page'] = $page;
        $requestBody['page_size'] = $page_size;
        if(!empty($department_code)){
            $requestBody['dept_id'] = $department_code;
        }
        if(!empty($starttime) && !empty($endtime)){
            $requestBody['starttime'] = $starttime;
            $requestBody['endtime'] = $endtime;
        }
        $request = array(
            'requestHead'   => $this->requestHead($method),
            'requestBody'   => $requestBody
            );
        $url = $this->apiConfig['request_url'].$methodUrl;
        $request = json_encode($request);
        $headers = array(
            "Host: 10.46.12.150:8820",
            "Connection: keep-alive",
            "Pragma: no-cache",
            "Cache-Control: no-cache",
            "DNT: 1",
            "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36",
            "Content-type: application/json",
            "Accept: */*",
            "Origin: chrome-extension://aejoelaoggembcahagimdiliamlcdmfm",
            "Accept-Encoding: gzip, deflate",            
            "Accept-Language: zh-CN,zh;q=0.9,zh-TW;q=0.8",
        );
        // print_r($request);
        $response = post_url($url, $request, $headers);
        // addlog($response);
        // echo $page;
        // echo '<br/>';
        // print_r($response);exit;
        $response = json_decode($response, true);
        if(is_array($response) && $response['resultCode'] == 0){
            $list = $response['resultSet'];
            if(is_array($list) && !empty($list)){
                foreach($list as $k=>$v){
                    $res = $this->savePatient($v);
                }
                $page++;
                $this->syncPatientList($page, $page_size, $department_code, $starttime, $endtime);
            }

        }
    }

    public function syncPatientDetail($patient_id){
        $method = 'patient_detail';
        $methodUrl = 'api/values/patient_detail';
        $requestBody = array();
        $requestBody['patient_id'] = $patient_id;
        $request = array(
            'requestHead'   => $this->requestHead($method),
            'requestBody'   => $requestBody
            );
        $url = $this->apiConfig['request_url'].$methodUrl;
        $response = post_url($url, $request);
        $response = json_decode($response, true);
        if(is_array($response) && $response['resultCode']==0){
            $res = $this->savePatient($response['resultSet']);
        }
    }

    private function savePatient($data){
        if(!is_array($data)){
            return false;
        }
        
        $saveData = array();

        $saveData['patient_id']     = trim($data['patient_id']);
        $saveData['patient_name']   = $data['patient_name'];
        $saveData['gender']     = $data['gender'];
        $saveData['age']    = $data['age'];
        $saveData['department_code'] = $data['department_code'];
        $saveData['department']     = $data['department'];
        $saveData['status']     = $data['status'];
        $saveData['rfid']   = $data['rfid'];
        
        if(!empty($data['nursing_class'])){
            $saveData['nursing_class']  = $data['nursing_class'];
        }
        if(!empty($data['bed'])){
            $saveData['bed']  = $data['bed'];
        }
        if(!empty($data['in_date'])){
            $saveData['in_date']  = $data['in_date'];
        }
        if(!empty($data['out_date'])){
            $saveData['out_date']  = $data['out_date'];
        }
        if(!empty($data['days'])){
            $saveData['days']  = $data['days'];
        }
        if(!empty($data['in_dia'])){
            $saveData['in_dia']  = $data['in_dia'];
        }
        if(!empty($data['doctor'])){
            $saveData['doctor']  = $data['doctor'];
        }
        if(!empty($data['contact_phone'])){
            $saveData['contact_phone']  = $data['contact_phone'];
        }
        // dump($saveData);exit;
        $patient = Db::name('patient')->field('id')->where(array('patient_id'=>$saveData['patient_id']))->find();
        if($patient){
            $res = Db::name('patient')->where(array('id'=>$patient['id']))->update($saveData);
        }else{
            $res = Db::name('patient')->insert($saveData);
        }

        return $res;
    }

    private function requestHead($method=''){
        $requestHead = array(
            'account_id'    => $this->apiConfig['account_id'],
            'method'        => $method,
            'timestamp'     => $this->timestamp2,
            'version'       => $this->apiConfig['version'],
            'sign'          => $this->makeSign($method)
        );

        return $requestHead;
    }

    private function makeSign($method){
        $str = $this->apiConfig['account_id'].$method.$this->timestamp2.$this->apiConfig['version'].$this->apiConfig['account_key'];
        $sign = strtolower(md5($str));
        return $sign;
    }


}